package com.raitcon.hibernate.db;

import com.raitcon.hibernate.bean.HibernateSessionFactory;
import com.raitcon.hibernate.bean.Alumno;
import com.raitcon.hibernate.factory.HibernateUtil;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class AlumnoDB {

    protected static Logger log = Logger.getLogger(AlumnoDB.class);
    private Session session = null;

    public AlumnoDB() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public Integer insertAlumno(Alumno alum) {
        int result = 0;
        try {
            System.out.println("metodo insert");
            session = HibernateSessionFactory.getSession();
            System.out.println("session: " + session);
            System.out.println("Try");
            Transaction ts = session.beginTransaction();
            System.out.println("Transaction");
            session.save(alum);
            System.out.println("Save");
            ts.commit();
            System.out.println("Se inserto correctamente");
            result = 1;
            return result;
        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
            return result;
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
            return result;
        } finally {
            session.close();
        }
    }

    public Integer updateAlumno(Alumno alum, long id) throws HibernateException, Exception {
        int result = 0;
        try {
            System.out.println("metodo update");
            session = HibernateSessionFactory.getSession();
            System.out.println("try");
            Transaction ts = session.beginTransaction();
            System.out.println("Transaction");
            Alumno proUpdate = (Alumno) session.get(Alumno.class, id);
            session.update(proUpdate);
            System.out.println("Update");
            ts.commit();
            System.out.println("Se modifico correctamente");
            result = 1;
            return result;
        } catch (HibernateException ex1) {
            return result;
        } catch (Exception ex2) {
            return result;
        } finally {
            session.close();
        }

    }

    public Integer deleteAlumno(Alumno alum) throws Exception {
        int result = 0;
        session = HibernateSessionFactory.getSession();
        try {
            Transaction ts = session.beginTransaction();
            session.delete(alum);
            ts.commit();
            System.out.println("Se elimno correctamente");
            result = 1;
            return result;
        } catch (HibernateException ex1) {
            return result;
        } catch (Exception ex2) {
            return result;
        } finally {
            session.close();
        }

    }

    public List getAlumnoAll() {
        List<Alumno> alumnoList = null;
        try {
            session = HibernateSessionFactory.getSession();
            Transaction ts = session.beginTransaction();
            Query q = session.createQuery("FROM  Alumno as alumno WHERE  state='1'");
            alumnoList = (List<Alumno>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alumnoList;
    }

    public List getAlumnoById(Integer codAlu) {

        List<Alumno> alumList = null;
        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Alumno WHERE id_alumno ='" + codAlu + "'");

            alumList = (List<Alumno>) q.list();
        } catch (Exception e) {
            log.debug("Error:" + e.getMessage());
            e.printStackTrace();
        } finally {
            session.close();
        }
        return alumList;
    }
    
     public List getAlumnoByDNI(String dni) {

        List<Alumno> alumList = null;
        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Alumno WHERE dni_alumno ='" + dni + "'");

            alumList = (List<Alumno>) q.list();
        } catch (Exception e) {
            log.debug("Error:" + e.getMessage());
            e.printStackTrace();
        } finally {
            session.close();
        }
        return alumList;
    }
}
